<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>Hash | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Hash</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">January 13, 2021&nbsp;&nbsp;0:00:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/%E7%AC%94%E8%AE%B0/">笔记</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><p>把任意数据转换成指定⼤⼩范围（通常很⼩，例如 256 字节以内）的数据。</p>
<h3 id="作⽤"><a href="#作⽤" class="headerlink" title="作⽤"></a>作⽤</h3><p>相当于从数据中提出摘要信息，因此最主要⽤途是数字指纹。</p>
<h3 id="Hash-的实际⽤途：唯⼀性验证"><a href="#Hash-的实际⽤途：唯⼀性验证" class="headerlink" title="Hash 的实际⽤途：唯⼀性验证"></a>Hash 的实际⽤途：唯⼀性验证</h3><p>例如 Java 中的 hashCode() ⽅法。</p>
<h4 id="怎么重写-hashCode-⽅法？"><a href="#怎么重写-hashCode-⽅法？" class="headerlink" title="怎么重写 hashCode ⽅法？"></a>怎么重写 hashCode ⽅法？</h4><p>把 equals() ⽅法中的每个⽤于判断相等的变量都放进 hashCode() 中，⼀起⽣成⼀个尽量不会碰撞的整数即可</p>
<ul>
<li>为什么每次重写 equals() ⽅法都需要？因为你要把新的判断条件放进hashCode() 啊。</li>
</ul>
<h3 id="Hash-的实际⽤途：数据完整性验证"><a href="#Hash-的实际⽤途：数据完整性验证" class="headerlink" title="Hash 的实际⽤途：数据完整性验证"></a>Hash 的实际⽤途：数据完整性验证</h3><p>从⽹络上下载⽂件后，通过⽐对⽂件的 Hash 值（例如 MD5、 SHA1），可以确认下载的⽂件是否有损坏。如果下载的⽂件 Hash 值和⽂件提供⽅给出的 Hash 值⼀致，则证明下载的⽂件是完好⽆损的。</p>
<h3 id="Hash-的实际⽤途：快速查找"><a href="#Hash-的实际⽤途：快速查找" class="headerlink" title="Hash 的实际⽤途：快速查找"></a>Hash 的实际⽤途：快速查找</h3><p>HashMap</p>
<h3 id="Hash-的实际⽤途：隐私保护"><a href="#Hash-的实际⽤途：隐私保护" class="headerlink" title="Hash 的实际⽤途：隐私保护"></a>Hash 的实际⽤途：隐私保护</h3><p>当重要数据必须暴露的时候，有事可以选择暴露它的 Hash 值（例如 MD5），以保障原数据的安全。例如⽹站登录时，可以只保存⽤户密码的 Hash 值，在每次登录验证时只需要将输⼊的密码的 Hash 值和数据库中保存的 Hash 值作⽐对就好，⽹站⽆需知道⽤户的密码。这样，当⽹站数据失窃时，⽤户不会因为⾃⼰的密码被盗导致<br>其他⽹站的安全也受到威胁。</p>
<ul>
<li>注意：这不属于加密。</li>
</ul>
<h3 id="Hash-是编码吗？"><a href="#Hash-是编码吗？" class="headerlink" title="Hash 是编码吗？"></a>Hash 是编码吗？</h3><p>不是。 Hash 是单向过程，往往是不可逆的，⽆法进⾏逆向恢复操作，因此 Hash 不属于编码。</p>
<h3 id="Hash-是加密吗？"><a href="#Hash-是加密吗？" class="headerlink" title="Hash 是加密吗？"></a>Hash 是加密吗？</h3><p>不是。 Hash 是单向过程，⽆法进⾏逆向回复操作，因此 Hash 不属于加密。（记住， MD5 不是加密！）</p>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2021/01/13/Hash/">https://zhangpingv5.gitee.io/2021/01/13/Hash/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/%E7%AC%94%E8%AE%B0/"># 笔记</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2021/01/15/%E7%99%BB%E5%BD%95%E5%92%8C%E7%AC%AC%E4%B8%89%E2%BD%85%E6%8E%88%E6%9D%83/">登录和第三⽅授权</a>
            
            
            <a class="next" rel="next" href="/2021/01/12/%E7%BC%96%E7%A0%81/">编码</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
